<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对接网关</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../js/element-ui-2.13.0/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/index.css"/>
    <link rel="stylesheet" href="../../styles/icon/iconfont.css"/>
    <link rel="stylesheet" href="../../styles/pages.css"/>
</head>
<body>
<div class="dashboard-container" id="category-app">
    <div class="container">
        <div
                class="tableBar"
                style="display: inline-block"
        >
            <el-input
                    v-model="company"
                    placeholder="请输入公司名称"
                    style="width: 250px"
                    clearable
                    @input="handleQuery"
            >
            </el-input>
            <el-select v-model="ipId" filterable placeholder="请选择IP地址" @change="handleQuery" clearable>
                <el-option
                        v-for="item in ipList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-select v-model="frontId" filterable placeholder="请选择落地" @change="handleQuery" clearable>
                <el-option
                        v-for="item in frontList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-button
                    type="primary"
                    @click="addEnd('end')"
            >
                + 新增对接网关
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
                @sort-change="handleSortChange"
        >
            <el-table-column
                    prop="company"
                    label="公司名"
                    sortable="true"
                    width="150px"
                    click="sendProperty(scope.row.company)"
            />
            <template slot-scope="scope">
                <el-link @click="sendProperty(scope.row.company)" type="info" target='_parent'>{{scope.row.company}}
                </el-link>
            </template>
            </el-table-column>
            <el-table-column
                    prop="ip"
                    label="IP地址"
                    width="250px"
            />
            <template slot-scope="scope">
                <el-link  type="info" target="_blank" :href="scope.row.ip">{{scope.row.ip}}</el-link>
            </template>
            </el-table-column>
            <el-table-column
                    prop="information"
                    label="客户信息"
                    width="200px"
            />
            <template slot-scope="scope">
                <el-button
                        type="text"
                        size="small"
                        class="blueBug"
                        @click="jointInformation(scope.row.joint)"
                >
                    对接信息
                </el-button>
                <el-button
                        type="text"
                        size="small"
                        class="blueBug"
                        @click="payInformation(scope.row.information)"
                >
                    查费信息
                </el-button>
            </template>
            </el-table-column>
            <el-table-column
                    prop="price"
                    label="价格(元)"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="front"
                    label="落地网关"
                    show-overflow-tooltip="true"
            />
            </el-table-column>
            <el-table-column
                    prop="numberCounts"
                    label="号码个数"
                    sortable="true"
            />
            <template slot-scope="scope">
                <el-link  type="info" @click="queryNumbers(scope.row)">{{scope.row.numberCounts}}</el-link>
            </template>
            </el-table-column>
            <el-table-column
                    prop="whiteCounts"
                    label="白名单数"
            />
            <template slot-scope="scope">
                <el-link  type="info" @click="showWhite(scope.row)">{{scope.row.whiteCounts}}</el-link>
            </template>
            </el-table-column>
            <el-table-column
                    prop="updateTime"
                    label="更新时间"
                    width="200px"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="160"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="greenBug"
                            @click="addWhite(scope.row.id)"
                    >
                        加白
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="editHandle(scope.row)"
                    >
                        修改
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="delBut non"
                            @click="deleteHandle(scope.row.id)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog
            :title="endParams.informationTitle"
            :visible.sync="endParams.informationVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-input
                type="textarea"
                :rows="5"
                v-model="information"
        />
    </el-dialog>
    <el-dialog
            :title="endParams.whiteTitle"
            :visible.sync="endParams.whiteDialogVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-form
                class="demo-form-inline"
                label-width="100px"
                :model="endParams"
                ref="endParams"
        >
            <el-form-item
                    label="白名单："
                    prop="whiteNumber"
            >
                <el-input
                        type="textarea"
                        :rows="10"
                        v-model="whiteNumber"
                />
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer"
        >
        <el-button
                size="medium"
                @click="handleClose()"
        >取 消</el-button>
        <el-button
                type="primary"
                size="medium"
                @click="submitForm()"
        >确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
            :title="endParams.title"
            :visible.sync="endParams.dialogVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-form
                class="demo-form-inline"
                label-width="100px"
                :model="endParams"
                ref="endParams"
        >
            <el-form-item
                    label="公司名："
                    prop="customerId"
                    :rules="[
                      { required: true, message: '公司名不能为空', trigger: 'blur' }]">
                <el-select v-model="endParams.customerId" filterable  placeholder="请选择公司名" @change="handleQuery" clearable>
                    <el-option
                            v-for="item in companies"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item
                    label="IP地址"
                    prop="ipId"
            >
                <el-select v-model="endParams.ipId" filterable placeholder="请选择">
                    <el-option
                            v-for="item in ipList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item
                    label="价格："
                    prop="price"
                    :rules="[
                      { required: true, message: '请输入价格', trigger: 'blur' },
                      { pattern: /^\d+\.?\d{0,4}/, message: '请输入正确的价格', trigger: ['blur', 'change'] }
                    ]"
            >
                <el-input
                        v-model="endParams.price"
                        placeholder="请输入价格(元)"
                        maxlength="14"
                        autocomplete
                />
            </el-form-item>
            <el-form-item
                    label="落地网关"
                    prop="frontIdList"
            >
                <el-select v-model="endParams.frontIdList" filterable multiple placeholder="请选择落地" @change="handleQuery" clearable>
                    <el-option
                            v-for="item in frontList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer"
        >
        <el-button
                size="medium"
                @click="handleClose()"
        >取 消</el-button>
        <el-button
                type="primary"
                size="medium"
                @click="submitForm()"
        >确 定</el-button>
        <el-button
                v-if="action != 'edit'"
                type="primary"
                size="medium"
                class="continue"
                @click="submitForm('go')"
        > 保存并继续添加 </el-button>
      </span>
    </el-dialog>
</div>
<script src="../../js/vue.js"></script>
<script src="../../js/element-ui-2.13.0/lib/index.js"></script>
<script src="../../js/axios-0.18.0.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/query.js"></script>
<script>
    new Vue({
        el: '#category-app',
        data() {
            return {
                indexPage: '../../index/index.html',
                action: '',
                counts: 0,
                pageNum: 1,
                pageSize: 10,
                company: '',
                tableData: [],
                companies: [],
                frontIdList: [],
                frontList: [],
                information: '',
                whiteNumber: '',
                price: '',
                type: '',
                orderByColumn: "updateTime",
                isAsc: "desc",
                ipList: [],
                endParams: {
                    'title': '',
                    'informationTitle': '',
                    'whiteTitle': '',
                    'whiteDialogVisible': false,
                    'dialogVisible': false,
                    'informationVisible': false,
                    'customerId': '',
                    'updateTime': '',
                    'ipId': '',
                    'information': '',
                    'frontIdList': [],
                    'whiteNumber': '',
                    'price': ''
                }
            }
        },
        computed: {},
        created() {
            this.init();
        },
        mounted() {
        },
        methods: {
            queryNumbers(row){
                localStorage.setItem("endId",row.id);
                localStorage.setItem("jump",'9');
                parent.location.href=this.indexPage;
            },
            sendProperty(company) {
                localStorage.setItem("company", company);
                localStorage.setItem("jump", '1');
                parent.location.href = this.indexPage;
            },
            async init() {
                this.ipList = []
                await queryAllIp({
                    'pageSize': 10000,
                    'pageNum': 1,
                    'ipType': '对接'
                }).then(res => {
                    res.result.data.forEach(item => {
                        let arr = {
                            value: item.ipId,
                            label: item.ip
                        };
                        this.ipList.push(arr);
                    });
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                this.frontList = []
                await queryFrontProperty({}).then(res => {
                    res.result.forEach(item => {
                        let arr = {
                            value: item.id,
                            label: item.front
                        };
                        this.frontList.push(arr);
                    });
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                this.companies = []
                await queryAllCustomer({
                    'pageSize': 10000,
                    'pageNum': 1
                }).then(res => {
                    res.result.data.forEach(item => {
                        let arr = {
                            value: item.id,
                            label: item.company
                        };
                        this.companies.push(arr);
                    });
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                await queryAllEnd({
                    'pageNum': this.pageNum,
                    'pageSize': this.pageSize,
                    'company': this.company,
                    'frontId': this.frontId,
                    'ipId': this.ipId,
                    'orderByColumn': this.orderByColumn,
                    'isAsc': this.isAsc
                }).then(res => {
                    if (String(res.code) == 200) {
                        this.tableData = res.result.data
                        this.counts = Number(res.result.total)
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.pageNum = 1;
                this.init();
            },
            // 添加
            addEnd(st) {
                if (st == 'end') {
                    this.endParams.title = '新增对接网关'
                }
                this.action = 'add'
                this.endParams.customerId = ''
                this.endParams.id = ''
                this.endParams.ipId = ''
                this.endParams.frontIdList = []
                this.endParams.price = ''
                this.endParams.dialogVisible = true
            },
            editHandle(dat) {
                this.endParams.title = '修改对接网关'
                this.action = 'edit'
                this.endParams.customerId = dat.customerId
                this.endParams.id = dat.id
                this.endParams.ipId = dat.ipId
                this.endParams.price = dat.priceDouble
                this.endParams.frontIdList = dat.frontIdList
                this.endParams.numberId = dat.numberId
                this.endParams.dialogVisible = true
            },
            addWhite(id){
                this.endParams.whiteTitle = '加白号码'
                this.action = 'addWhite'
                this.endParams.id = id
                this.whiteNumber = ''
                this.endParams.whiteDialogVisible = true
            },
            showWhite(endVo){
                this.endParams.whiteTitle = '白名单'
                this.whiteNumber = endVo.whiteNumber
                this.action = 'updateWhite'
                this.endParams.id = endVo.id
                this.endParams.whiteDialogVisible = true
            },
            payInformation(dat){
                this.endParams.informationTitle = '查费信息'
                this.information = dat
                this.endParams.informationVisible = true
            },
            jointInformation(dat){
                this.endParams.informationTitle = '对接信息'
                this.information = dat
                this.endParams.informationVisible = true
            },
            // 关闭弹窗
            handleClose(st) {
                this.endParams.dialogVisible = false
                this.endParams.informationVisible = false
                this.endParams.whiteDialogVisible = false
                this.$refs['endParams'].resetFields()
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteEnd({'id': id}).then(res => {
                        if (res.code === 200) {
                            this.$message.success('删除成功！')
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                this.$refs[`endParams`].validate((valid) => {
                    if (valid) {
                        const endParams = this.endParams
                        endParams.endType = this.endType
                        endParams.userId = this.userId
                        if (this.action === 'add') {
                                addEnd(endParams).then(res => {
                                    if (res.code === 200) {
                                        this.$message.success('对接网关添加成功！')
                                        if (!st) {
                                            this.endParams.dialogVisible = false
                                        }
                                        this.init()
                                    } else {
                                        this.$message.error(res.msg || '操作失败')
                                    }
                                }).catch(err => {
                                    this.$message.error('请求出错了：' + err)
                                })
                        } else if (this.action === 'edit') {
                            endParams.whiteNumber = '-1'
                            updateEnd(endParams).then(res => {
                                if (res.code === 200) {
                                    this.$message.success('对接网关修改成功！')
                                    if (!st) {
                                        this.endParams.dialogVisible = false
                                    }
                                    this.init()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }else if (this.action === 'addWhite'){
                            this.endParams.whiteNumber = this.whiteNumber
                            addWhiteNumber(endParams).then(res => {
                                if (res.code === 200) {
                                    this.$message.success(res.msg)
                                    if (!st) {
                                        this.endParams.whiteDialogVisible = false
                                    }
                                    this.init()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }else if (this.action === 'updateWhite'){
                            endParams.whiteNumber = this.whiteNumber
                            updateEnd(endParams).then(res => {
                                if (res.code === 200) {
                                    this.$message.success(res.msg)
                                    if (!st) {
                                        this.endParams.whiteDialogVisible = false
                                    }
                                    this.init()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    }
                });
            }
            ,
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            }
            ,
            handleCurrentChange(val) {
                this.pageNum = val
                this.init()
            }
            ,
            handleSortChange(val) {
                if (val.column) {
                    this.orderByColumn = val.prop
                    this.isAsc = val.order == 'ascending' ? 'ASC' : 'DESC'
                }
                this.init()
            }
        }
    })
</script>
</body>
</html>